<template>
	<div id="app">
		<!--<scroll ID='sidebar__' class='sssss'>-->
		<keep-alive>
			<router-view ref='roterview' class="child-view" v-if="$route.meta.keepAlive && isRouterAlive"></router-view>
		</keep-alive>
		<router-view ref='roterview' class="child-view" v-if="!$route.meta.keepAlive && isRouterAlive"></router-view>
		<!--</scroll>-->

		<mapset :model="mapmodel" ref="mapset" @selectBack="selectAddressBack"></mapset>
	</div>
</template>

<script>
	import './element-variables.scss';
	import bus from '@/js/common/bus.js';
	import scroll from '@/components/scroll';
	import mapset from '@/components/map/index.vue';
	export default {
		name: 'App',
		components: {
			scroll,
			mapset
		},
		provide() {
			return {
				reload: this.reload,
				closeLoading: this.closeLoading,
				openLoading: this.openLoading,
				hasPermission: this.hasPermission,
				isPersionAccount: this.isPersionAccount,
				selectAddressShow: this.selectAddressShow,
				changeRouter:this.changeRouter
			}
		},
		data() {
			return {
				isRouterAlive: true,
				isLoadingShow: true,
				isLoadingShows: false,
				mapmodel: {
					visible: true,
					address: null,
					latitude: 0,
					longitude: 0,
				}
			}
		},
		mounted() {
			if(this.tool.getToken()) {
				this.getUserInfo();
			}
			bus.$on('changeValue', res => {
				let arr_key = res.arr_key
				for(let key_ of arr_key) {
					let data = res.data[key_];
					let obj = {
						name: key_,
						value: data
					}
					this.$store.commit('changeValue', obj);
				}
			})
			bus.$on('message', res => {
				if(res.code == 21 || res.code == 17) {
					this.$store.commit('changeValue', {
						name: 'userInfo',
						value: null
					})
					this.$router.push({
						path: '/login'
					});
				} else if(res.code != 1) {
					this.$message.error(res.msg);
				}
			});
			bus.$on('changeRouter', url => {
				this.$router.push(url);
			});
			bus.$on('replaceRouter', url => {
				this.$router.replace(url);
			});

		},
		methods: {
			reload() {
				this.isRouterAlive = false;
				this.$nextTick(() => {
					this.isRouterAlive = true;
				})
			},
			selectAddressShow(obj) {
				console.log(obj)
				this.$refs.mapset.show(obj);
			},
			selectAddressBack(obj) {
				bus.$emit('test', obj);
			},
			async getUserInfo() {
				let checkToken = await this.httptool.post('customer/checkToken', null);
				if(checkToken.data == true) {
					this.tool.cleanToken();
					return;
				}
				let res = await this.httptool.post(this.apis.getCustomerDetail, null);
				this.$store.commit('changeValue', {
					name: 'userInfo',
					value: res.data
				})
			},
			handler({
				BMap,
				map
			}) {
				this.center.lng = 118.1948711891;
				this.center.lat = 24.4810250498;
				this.zoom = 15;
				this.getLocation();
				this.$store.commit('changeValue', {
					name: 'BMap',
					value: BMap
				})
			},
			getLocation() {
				//定位
				let that = this;
				var geolocation = new BMap.Geolocation();
				geolocation.getCurrentPosition(function(r) {
					if(this.getStatus() == BMAP_STATUS_SUCCESS) {
						let obj = {
							longitude: r.point.lng,
							latitude: r.point.lat,
							address: r.address.province + r.address.city,
							city: r.address.city
						}
						that.$store.commit('changeValue', {
							name: 'geolocation',
							value: obj
						})

					}
				}, {
					enableHighAccuracy: true
				}); //设置当前位置
			},
			hasPermission(type) {
				let tip = true;
				if(type === null || type === undefined) {
					tip = true;
				}
				let userInfo = this.$store.state.userInfo;
				if(userInfo && userInfo.id) {
					//type 0 是否能添加子账号(是否为个人用户) 1下单 2支付 3 退款 4取消订单 5发标
					if(userInfo.type == 0) { //个人买家
						tip = false; //个人买家				
					} else if(userInfo.type == 1 && !userInfo.isMasterAccount) {
						if(type == 0) {
							tip = true;
						}
						if(!userInfo.permissionIds) {
							tip = false;
						} else {
							let index = userInfo.permissionIds.indexOf(type);
							if(index > -1) {
								tip = true;
							} else {
								tip = false;
							}
						}
					}else{
						tip = true;
					}
					
				}
				return new Promise((resolve, reject) => {
					if(tip) {
						resolve()
					} else {
						this.$message('暂无权限！');
					}
				})
			},
			isPersionAccount() {
				let userInfo = this.$store.state.userInfo;
				if(userInfo && userInfo.id) {
					if(userInfo.type == 0) {
						return true;
					} else {
						return false;
					}
				}
				return true;
			},
			changeRouter(url){
				this.$router.push(url);
			}
		}
	};
</script>

<style lang="less">
	#app {
		font-family: 'Avenir', Helvetica, Arial, sans-serif;
		-webkit-font-smoothing: antialiased;
		-moz-osx-font-smoothing: grayscale;
		/*height: 100vh;*/
	}
	
	.ul_ {
		overflow: hidden;
		height: 100%;
	}
	
	.map {
		height: 4px;
		width: 1px;
		position: fixed;
		top: 1px;
		left: 1px;
	}
</style>